import JSResource from "../JSResource";
import {loadQuery, } from "react-relay/hooks";
import {Environment} from "relay-runtime";
import importedComponent from "react-imported-component";

/**
 * 多层次的路由器。
 * require（）目录文件无效的不报错，但是路由secondRouter会运行出错加载父文件export失败
 * */
export default function secondRouter(environment: Environment) {
  return (
      {
        path: '/test',
        //exact: true,  //多层路由嵌套只能去掉 true
        component: importedComponent(() => import('./Test')),
        prepare: (params:any) => {
           //底下这个请求完全可以合并成一个请求包的：从graphql定义入手归并。
          const TestQuery9 = require('./__generated__/TestRootIssuesQuery.graphql');
          const TestRootIssues = require('./__generated__/TestRootIssuesQuery.graphql');
          return {
            testQuery9: loadQuery(
                environment,
                TestQuery9,
                {
                  id: 20,
                  username: 'herzhang'
                },
                { fetchPolicy: 'store-or-network' },
            ),
            testRootIssues: loadQuery(
                environment,
                TestRootIssues,
                {
                  id: 1520275,
                  username: 'hez'
                },
                { fetchPolicy: 'store-or-network' },
            ),
          };
        },
        routes:[
            {
                path: '/test/old',
                exact: true,
                component: importedComponent(() => import('./sons/Remod')),
                prepare: (params:any) => {
                    const TestQuery9 = require('./sons/__generated__/RemodQuery.graphql');
                    const TestRootIssues = require('./sons/__generated__/RemodQuery.graphql');
                    return {
                        testQuery9: loadQuery(
                            environment,
                            TestQuery9,
                            {
                                id: 3,
                                repId: '1520286'
                            },
                            { fetchPolicy: 'store-or-network' },
                        ),
                        testRootIssues: loadQuery(
                            environment,
                            TestRootIssues,
                            {
                                id: 1520275,
                                repId: '6'
                            },
                            { fetchPolicy: 'store-or-network' },
                        ),
                    };
                },
            },
            {
            path: '/test/remod',
            exact: true,
            component: importedComponent(() => import('./sons/Remod')),
            prepare: (params:any) => {
              const TestQuery9 = require('./sons/__generated__/RemodQuery.graphql');
              const TestRootIssues = require('./sons/__generated__/RemodQuery.graphql');
              return {
                 //传递给路由层次组件的参数:
                testQuery9: loadQuery(
                    environment,
                    TestQuery9,
                    {
                      id: 1520275,
                      repId: '1520287'
                    },
                    { fetchPolicy: 'store-or-network' },
                ),
                testRootIssues: loadQuery(
                    environment,
                    TestRootIssues,
                    {
                      id: 20,
                      repId: '99077'
                    },
                    { fetchPolicy: 'store-or-network' },
                ),
              };
            },
            },
            {
                path: '/test/last',
                exact: true,
                component: importedComponent(() => import('./sons/Remod')),
                prepare: (params:any) => {
                    const TestQuery9 = require('./sons/__generated__/RemodQuery.graphql');
                    const TestRootIssues = require('./sons/__generated__/RemodQuery.graphql');
                    return {
                        testQuery9: loadQuery(
                            environment,
                            TestQuery9,
                            {
                                id: 20,
                                repId: '1520286'
                            },
                            { fetchPolicy: 'store-or-network' },
                        ),
                        testRootIssues: loadQuery(
                            environment,
                            TestRootIssues,
                            {
                                id: 1520275,
                                repId: '1520287'
                            },
                            { fetchPolicy: 'store-or-network' },
                        ),
                    };
                },
            },
        ],
      }
  );
}

//export default SecondRouter();

